<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\model\AdminPhoto as P;
use app\admin\service\AdminAdmin as S;
use app\common\util\UploadUtil as Up;
use think\facade\Request;
use think\facade\Session;

class Index extends Base
{
    protected $middleware = ['AdminCheck'];
    
    // 首页
    public function index(){
        return $this->fetch('', [
            'nickname' => getField('admin_admin', Session::get('admin.id'), 'nickname'),
            'avatar' => getField('admin_admin', Session::get('admin.id'), 'avatar'),
            'addr' => $this->getSystem()['addr'],
        ]);
    }

    // 清除缓存
    public function cache(){
        Session::clear();
         return $this->getJson(rm());
        }

    // 菜单
    public function menu(){
        return json(getTree(Session::get('admin.menu')));
    }

    // 欢迎页
    public function home(){
        return $this->fetch('',$this->getSystem());
    }

    // 修改密码
    public function pass(){
        if (Request::isAjax()){
            $this->getJson(S::goPass());
        }
        return $this->fetch();
    }

    public function upload()
    {
        return $this->getJson(Up::putFile(Request::file(), Request::get('type'), Request::get('path'), Request::get('save'), Request::get('local'), $this->global_config));
    }

    // 图库选择
    public function optPhoto($name = '', $multiple = 0)
    {
        if (Request::isAjax()) {
            return $this->getJson(P::getAll());
        }
        $this->assign('name', $multiple ? $name . '[]' : $name);
        $this->assign('multiple', $multiple);
        return $this->fetch('', P::getPath());
    }

    public function chooseLocation()
    {
        $lng = input('param.lng');
        $lat = input('param.lat');
        $name = input('param.name');
        $this->assign('lng', $lng);
        $this->assign('lat', $lat);
        $this->assign('name', $name);
        return $this->fetch();
    }

    // 选择位置
    public function imageCropper()
    {
        $image = input('param.image') ?: '';
        $this->assign('path', input('param.path'));
        $this->assign('save', input('param.save'));
        $this->assign('image', $image);
        $this->assign('width', input('param.width'));
        $this->assign('height', input('param.height'));
        $this->assign('size', config('procedure.uploadValidate.imgValidate.validate.size'));
        return $this->fetch();
    }

    //刷新表单__token__
    public function getToken()
    {
        $request = \think\facade\Request::instance();
        echo $request->buildToken('__token__', 'sha1');
    }
}
